#include "class_strategy_spread.h"
#include "class_center.h"

extern class_center a;
extern lambda_pool pool;


class_strategy_spread::class_strategy_spread(int idd, std::string dm, int n1, double bj, int sl)
{
memset(&ints,0,sizeof(ints));
memset(&dous,0,sizeof(dous));

memset(&data_a, 0, sizeof(CThostFtdcDepthMarketDataField) );
memset(&data_b, 0, sizeof(CThostFtdcDepthMarketDataField) );

name = dm;


size_t jjj=name.find("--");
name_a=name.substr(0,jjj);
name_b=name.substr(jjj+2,name.size()-jjj-2);

std::string str = a.path_str + "/strategy";
//if(nullptr == opendir(str.c_str()))
//{
//auto return_value=mkdir(str.c_str(),777);
//if(return_value != 0)
//{
//	std::cout<<"建立目录:"<<str<<"失败！返回值:"<<return_value<<std::endl;
//}
//}
create_path(str);

str = a.path_str + "/strategy/spread";
//if(nullptr == opendir(str.c_str()))
//{
//auto return_value=mkdir(str.c_str(),777);
//if(return_value != 0)
//{
//	std::cout<<"建立目录:"<<str<<"失败！返回值:"<<return_value<<std::endl;
//}
//}
create_path(str);

str = a.path_str + "/strategy/spread/"+dm;
//if(nullptr == opendir(str.c_str()))
//{
//auto return_value=mkdir(str.c_str(),777);
//if(return_value != 0)
//{
//	std::cout<<"建立目录:"<<str<<"失败！返回值:"<<return_value<<std::endl;
//}
//}
create_path(str);

str = a.path_str + "/strategy/spread/"+dm+"/"+name_a;
//if(nullptr == opendir(str.c_str()))
//{
//auto return_value=mkdir(str.c_str(),777);
//if(return_value != 0)
//{
//	std::cout<<"建立目录:"<<str<<"失败！返回值:"<<return_value<<std::endl;
//}
//}
create_path(str);

str = a.path_str + "/strategy/spread/"+dm+"/"+name_b;
//if(nullptr == opendir(str.c_str()))
//{
//auto return_value=mkdir(str.c_str(),777);
//if(return_value != 0)
//{
//	std::cout<<"建立目录:"<<str<<"失败！返回值:"<<return_value<<std::endl;
//}
//}
create_path(str);

str = a.path_str + "/log/strategy";
//if(nullptr == opendir(str.c_str()))
//{
//auto return_value=mkdir(str.c_str(),777);
//if(return_value != 0)
//{
//	std::cout<<"建立目录:"<<str<<"失败！返回值:"<<return_value<<std::endl;
//}
//}
create_path(str);

str = a.path_str + "/log/strategy/spread";
//if(nullptr == opendir(str.c_str()))
//{
//auto return_value=mkdir(str.c_str(),777);
//if(return_value != 0)
//{
//	std::cout<<"建立目录:"<<str<<"失败！返回值:"<<return_value<<std::endl;
//}
//}
create_path(str);

ints[1] = n1;
ints[2] = sl;
dous[1]=bj;

cc = new class_strategy_position(a.path_str + "/strategy/spread/" + dm+"/"+name_a);
dd = new class_strategy_position(a.path_str + "/strategy/spread/" + dm+"/"+name_b);


bars_a.read(a.path_str+"/bars/" + name_a + ".dat", n1+202);
if(a.bars_map.find(name_a) != a.bars_map.end() )
{
for(auto &v: a.bars_map[name_a].datas)
{
bars_a.datas.push_back(v);
}
}

bars_b.read(a.path_str+"/bars/" + name_b + ".dat", n1+202);
if(a.bars_map.find(name_b) != a.bars_map.end() )
{
for(auto &v: a.bars_map[name_b].datas)
{
bars_b.datas.push_back(v);
}
}

//中轴·均线
dous[7] = bars_a.average(0,ints[1]);
dous[8] = bars_b.average(0,ints[1]);
dous[9]=dous[7]-dous[8];

id=idd;
ints[0] = cc->position;
//a.tick_event_map[name_a].insert([this](){run_one();},id);
//a.tick_event_map[name_a].insert([this](){ pool.insert([this](){run_one();}); },id);
a.tick_event_map[name_a].insert([this](){ pool.insert([this](){ if(a.flag[50]==1){run_one();} }); },id);

a.log.insert("strategy/spread/"+name,"实例化成功,当前仓位" + std::to_string(cc->position));
}

class_strategy_spread::~class_strategy_spread()
{

a.tick_event_map[name_a].pop(id);

a.log.insert("strategy/spread/"+name,"析构完成,当前仓位"+std::to_string(cc->position));
delete cc;
delete dd;
}


// 主执行函数
void class_strategy_spread::run_one()
{

//清理报单返回值
a.return_erase(*cc);
a.return_erase(*dd);

//同步持仓
a.position_sync(*cc);
a.position_sync(*dd);

//获取到TICK
data_a=a.tick_map[name_a];
data_b=a.tick_map[name_b];


//获取行情到bar
bars_a.insert(data_a);
bars_b.insert(data_b);

if (bars_a.datas.size() < (ints[1] + 2) || bars_b.datas.size() < (ints[1] + 2) || a.flag[35] == 0)
{
	std::cout<<"价差回归"<<name_a<<"bar数量"<<bars_a.datas.size()<<std::endl;
	std::cout<<"价差回归"<<name_b<<"bar数量"<<bars_b.datas.size()<<std::endl;
return;
}


//新的bar
if (bars_a.datas.size() != ints[8])
{
ints[8] = bars_a.datas.size();
//中轴·均线
dous[7] = bars_a.average(0,ints[1]);
}

if (bars_b.datas.size() != ints[9])
{
ints[9] = bars_b.datas.size();
dous[8] = bars_b.average(0,ints[1]);

a.log.insert("strategy/spread/" + name, "当前价差:" + std::to_string(data_a.LastPrice - data_b.LastPrice) +",价差均线:" + std::to_string(dous[7] - dous[8]) + ",a持仓:" + std::to_string(cc->position) + ",b持仓:" + std::to_string(dd->position));
}

dous[9]=dous[7]-dous[8];

//仓位计算
if(data_a.AskPrice1 - data_b.BidPrice1 < dous[9] - dous[1])
{
ints[0] = 1;
}

if(data_a.BidPrice1 - data_b.AskPrice1 > dous[9] + dous[1])
{
ints[0] = -1;
}


if(data_a.BidPrice1 - data_b.AskPrice1 > dous[9] && ints[0] == 1)
{
ints[0] = 0;
}

if(data_a.AskPrice1 - data_b.BidPrice1 < dous[9] && ints[0] == -1)
{
ints[0] = 0;
}


//报单动作
//如果处于买入区且仓位不够则
if (ints[0] == 1)
{
if(cc->position < ints[2] && cc->return_vector.size() == 0)
{
a.bd_auto(data_a.InstrumentID, '0', data_a.UpperLimitPrice, 1, *cc);
a.log.insert("strategy/spread/" + name +"交易",name_a + "多开动作" + std::to_string(data_a.AskPrice1));
}

if(dd->position > ints[2] * -1 && dd->return_vector.size() == 0)
{
a.bd_auto(data_b.InstrumentID, '1', data_b.LowerLimitPrice, 1, *dd);
a.log.insert("strategy/spread/" + name +"交易", name_b + "空开动作" + std::to_string(data_b.BidPrice1));
}
}


if(ints[0] == -1)
{

if(cc->position > 0 - ints[2] && cc->return_vector.size() == 0)
{
a.bd_auto(data_a.InstrumentID, '1', data_a.LowerLimitPrice, 1, *cc);
a.log.insert("strategy/spread/" + name + "交易",name_a + "空开动作" + std::to_string(data_a.BidPrice1));
}

if(dd->position < ints[2] && cc->return_vector.size() == 0)
{
a.bd_auto(data_b.InstrumentID, '0', data_b.UpperLimitPrice, 1, *dd);
a.log.insert("strategy/spread/" + name +"交易",name_b + "多开动作" + std::to_string(data_b.AskPrice1));
}
}


//平仓
if(ints[0] == 0)
{
if(cc->position > 0 && cc->return_vector.size() == 0)
{
a.bd_auto(data_a.InstrumentID, '1', data_a.LowerLimitPrice, 1,*cc);
a.log.insert("strategy/spread/" + name +"交易",name_a + "多平动作" + std::to_string(data_a.BidPrice1));
}
if(cc->position < 0 && cc->return_vector.size() == 0)
{
a.bd_auto(data_a.InstrumentID, '0', data_a.UpperLimitPrice, 1,*cc);
a.log.insert("strategy/spread/" + name +"交易", name_a + "空平动作" + std::to_string(data_a.AskPrice1));
}

if(dd->position > 0 && dd->return_vector.size() == 0)
{
a.bd_auto(data_b.InstrumentID, '1', data_b.LowerLimitPrice, 1,*dd);
a.log.insert("strategy/spread/" + name +"交易", name_b + "多平动作" + std::to_string(data_b.BidPrice1));
}

if(dd->position < 0 && dd->return_vector.size() == 0)
{
a.bd_auto(data_b.InstrumentID, '0', data_b.UpperLimitPrice, 1,*dd);
a.log.insert("strategy/spread/" + name +"交易", name_b + "空平动作" + std::to_string(data_b.AskPrice1));
}
}

return;
}
